//
//  WelcomeView.swift
//  MyLottery
//
//  Created by wujunjie on 2018/8/11.
//  Copyright © 2018年 wujunjie. All rights reserved.
//

import UIKit
import SDWebImage

/**
 1. 给控件添加完约束之让, 调用layoutIfNeeed方法,让约束立即生效.
 2. 在 didMoveToWindow 方法,添加动画: update 头像的约束, 调用layoutIfNeeed方法,让约束生效.
 */

//MARK: - 属性, 构造函数, 生命周期函数
class WelcomeView: UIView {
    /// 用户头像
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        //设置圆角
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
//        imageView.sd_setImage(UserInfoModel.userAccount.avatar_large!, placeHolder: "avatar_default_big")
//        imageView.sd_setImage(with: UserInfoModel.userAccount.avatar_large!, placeholderImage: "avatar_default_big", options: SDWebImageOptions, progress: { (Int , Int) in
//
//        }, completed: { (<#UIImage?#>, <#Error?#>, <#SDImageCacheType#>, <#URL?#>) in
//
//        })
        return imageView
    }()
    
    /// 欢迎文字
//    private lazy var welcomeLabel: UILabel = UILabel(text: "\(UserInfoModel.userAccount.screen_name!), 欢迎归来!", alightment: .center, fontSize: 18)
    
    /// 子视图添加到父视图时调用该方法
    override func didMoveToSuperview() {
        //addSubView时调用
        print(#function)
    }
    
    /// 它的controller的viewWillAppear执行之后调用
    override func didMoveToWindow() {
        print(#function)
        //添加弹力动画
        addAnimation()
    }
    
    /// 布局子视图. 该方会调用多次, 系统自动调用. 如果需要手动调用, 需要执行 layoutIfNeeded
    override func layoutSubviews() {
        print(#function)
    }
    
    init() {
        super.init(frame: kScreenBounds)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - 设置UI
extension WelcomeView {
    private func setupUI () {
        backgroundColor = UIColor.white
        
        //1. 添加子控件
        addSubview(iconImageView)
//        addSubview(welcomeLabel)
        
        //2. 自动布局
        iconImageView.snp.makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-50)
            make.size.equalTo(CGSize(width:90, height: 90))
        }
        
//        welcomeLabel.snp.makeConstraints { (make) -> Void in
//            make.centerX.equalTo(self)
//            make.centerY.equalTo(self).offset(100)
//        }
        
        //让布局立即生效
        layoutIfNeeded()
    }
    
    /// 添加弹力动画
    private func addAnimation () {
        //约束, 第一次创建时,使用的snp_makeConstraints, 第二修改,则需要使用 snp_updateConstraints, 更新约束,只需要修改, 要修改的值
        iconImageView.snp.updateConstraints { (make) -> Void in
            make.centerY.equalTo(self).offset(-180)
        }
        
        //usingSpringWithDamping: 弹力系数, 从0-1, 越小越弹
        //Velocity 重力系数 9.8
        UIView.animate(withDuration: 2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //让动画生效
            //该方法会强制调用layoutsubview. 一旦调用layoutSubviews,则约束才会生效
            self.layoutIfNeeded()
        }) { (success) -> Void in
            self.removeFromSuperview()
        }
    }
}
